package com.zxt.service.impl;

import com.zxt.dao.OrderDao;
import com.zxt.dao.impl.OrderDaoImpl;
import com.zxt.domain.*;
import com.zxt.service.AddressService;
import com.zxt.service.CartService;
import com.zxt.service.GoodsService;
import com.zxt.service.OrderService;
import com.zxt.util.DataSourceUtils;

import java.util.List;

public class OrderServiceImpl implements OrderService {
    private OrderDao orderDao=new OrderDaoImpl();
    @Override
    public void submitOrder(Order order, List<Cart> cartList) {
        try {
            //事务
            //开启事务
            DataSourceUtils.begin();
            //1.向订单表中添加订单
            orderDao.add(order);
            //2.向订单详情表添加数据
            for (Cart cart : cartList){
                OrderDetail orderDetail=new OrderDetail(0,order.getId(),cart.getPid(),cart.getNum(),cart.getMoney());
                orderDao.addDetail(orderDetail);
            }
            //3.清空购物车
            CartService cartService=new CartServiceImpl();
            cartService.clear(order.getUid());
            //提交事务
            DataSourceUtils.commit();
        }catch (Exception e){
            //事务回滚
            try {
                DataSourceUtils.rollback();
            } catch (Exception ex) {
              throw new RuntimeException(e);
            }
        }finally {
            try {
                DataSourceUtils.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void updateState(String oid, int state) {
        orderDao.updateState(oid,state);
    }

    @Override
    public List<Order> find(Integer uid) {
        List<Order> orderList=orderDao.select(uid);
        if (orderList != null&&orderList.size()>=0){
            //获取地址
            AddressService addressService=new AddressServiceImpl();
            List<Address> addList = addressService.find(uid);
            if (addList !=null){
                for (Order order : orderList){  //2  oid
                    for (Address address : addList){   //2  7  9  id
                        if (address.getId()==order.getAid()){
                            order.setAddress(address);
                        }

                    }
                }
            }
        }
        return orderList;
    }

    @Override
    public Order findByOid(String oid) {
        Order order=orderDao.selectByOid(oid);
        if (order !=null){
            AddressService addressService=new AddressServiceImpl();
            List<Address> addList = addressService.find(order.getUid());
            if (addList !=null){
                for (Address address : addList){
                    if (address.getId()==order.getAid()){
                        order.setAddress(address);
                    }
                }
            }
        }
        return order;
    }

    @Override
    public List<OrderDetail> findDetail(String oid) {
        List<OrderDetail> orderDetails=orderDao.selectDetail(oid);
        if (orderDetails !=null&&orderDetails.size()>0){
            GoodsService goodsService=new GoodsServiceImpl();
            for (OrderDetail orderDetail : orderDetails){
                Goods goods=goodsService.findById(orderDetail.getPid());
                orderDetail.setGoods(goods);
            }
        }
        return orderDetails;
    }

}